| Conditions | 5 |
| Total Lines | 104 |
| Code Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import React from 'react'; |
||
| 8 | |||
| 9 | export default function Wallet({navigation}): any { |
||
| 10 | const [history, setHistory] = useState([]); |
||
| 11 | const [modalVisible, setModalVisible] = useState(false); |
||
| 12 | const [currentJourney, setCurrentJourney] = useState(null); |
||
| 13 | const [noHistory, setNoHistory] = useState(false); |
||
| 14 | const [distance, setDistance] = useState(null) |
||
| 15 | |||
| 16 | |||
| 17 | async function getHistory(): Promise<void> { |
||
| 18 | const result = await userModel.getHistory(); |
||
| 19 | |||
| 20 | if (result.length === 0) { |
||
| 21 | setNoHistory(true); |
||
| 22 | return; |
||
| 23 | } |
||
| 24 | setHistory(result); |
||
| 25 | |||
| 26 | console.log(history); |
||
| 27 | |||
| 28 | // const travelDistance = mapModel.calcDistance(result['startPosition']['latitude'], result['startPosition']['longitudo'], result['endPosition']['latitude'], result['endPosition']['longitude']) |
||
| 29 | |||
| 30 | // setDistance(travelDistance); |
||
| 31 | |||
| 32 | setNoHistory(false); |
||
| 33 | }; |
||
| 34 | |||
| 35 | |||
| 36 | // Get history for logged in user |
||
| 37 | useEffect(() => { |
||
| 38 | getHistory(); |
||
| 39 | |||
| 40 | }, []); |
||
| 41 | |||
| 42 | // Reload history on focus |
||
| 43 | useEffect(() => { |
||
| 44 | navigation.addListener('focus', () => getHistory()) |
||
| 45 | }, []); |
||
| 46 | |||
| 47 | |||
| 48 | function getDistance(history) { |
||
| 49 | const travelDistance = mapModel.calcDistance(history['startPosition']['latitude'], history['startPosition']['longitude'], history['endPosition']['latitude'], history['endPosition']['longitude']); |
||
| 50 | |||
| 51 | return travelDistance.toFixed(2).toString(); |
||
| 52 | |||
| 53 | |||
| 54 | } |
||
| 55 | return ( |
||
| 56 | <View style={styles.container}> |
||
| 57 | |||
| 58 | <View style={styles.titleContainer}> |
||
| 59 | |||
| 60 | <Pressable style={[styles.backButton, styles.shadowProp]} onPress={() => navigation.navigate('Map')}> |
||
| 61 | <Icon |
||
| 62 | name='x' |
||
| 63 | size={25} |
||
| 64 | color='black' |
||
| 65 | /> |
||
| 66 | </Pressable> |
||
| 67 | |||
| 68 | <Text style={styles.title}>Ride History</Text> |
||
| 69 | |||
| 70 | <View style={{width: 50}}></View> |
||
| 71 | |||
| 72 | </View> |
||
| 73 | |||
| 74 | {noHistory ? |
||
| 75 | <Text style={{color: 'gray'}}>No travel history to show</Text> |
||
| 76 | : |
||
| 77 | <ScrollView style={styles.prepaidContainer}> |
||
| 78 | {history.map((h, index) => ( |
||
| 79 | <Pressable onPress={() => { |
||
| 80 | setCurrentJourney(h) |
||
| 81 | setModalVisible(true) |
||
| 82 | }} |
||
| 83 | key={index} |
||
| 84 | > |
||
| 85 | |||
| 86 | |||
| 87 | <View style={styles.rideHistory}> |
||
| 88 | |||
| 89 | <View style={styles.primaryInfo}> |
||
| 90 | <Text style={styles.textDistance}> |
||
| 91 | {getDistance(h)} km / {h.totalMin} min |
||
| 92 | </Text> |
||
| 93 | <Text style={styles.textCost}>{h.totalPrice} kr</Text> |
||
| 94 | </View> |
||
| 95 | |||
| 96 | <View style={styles.secondaryInfo}> |
||
| 97 | <Text style={styles.textDate}>{h.date}</Text> |
||
| 98 | </View> |
||
| 99 | </View> |
||
| 100 | </Pressable> |
||
| 101 | ))} |
||
| 102 | <HistoryMap navigation={navigation} journey={currentJourney} modalVisible={modalVisible} setModalVisible={setModalVisible}></HistoryMap> |
||
| 103 | </ScrollView> |
||
| 104 | |||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | |||
| 109 | |||
| 110 | |||
| 111 | </View> |
||
| 112 | ) |
||
| 271 | }); |